<?php
namespace app\common\model;

class ShopGoodsBrandModel extends BaseModel
{
    protected $name = 'shop_goods_brand';
    protected $autoWriteTimestamp = false;
    // 定义自动完成的属性
    protected $auto = []; //新增修改
    protected $insert = []; //插入
    protected $update = []; //修改

    public function getCidsTextAttr($value, $data)
    {
        $cids               = $data['cids'];
        $goodsCategoryModel = new ShopGoodsCategoryModel();
        $catName            = $goodsCategoryModel->where('cid', 'in', $cids)->column('cat_name');
        $catName            = implode(',', $catName);
        return $catName;
    }

    protected function getBrandContentAttr($value)
    {
        return stripslashes(htmlspecialchars_decode($value));
    }

    /*修改器*/
    public function cidBrandCache($siteId){
        $goodsCategory = goods_cache_cat($siteId);
        $cidBrand = [];
        if(!empty($goodsCategory)){
            foreach($goodsCategory as $k=>$v){
                if($v['pid'] == 0){
                    $cids = array_keys(\org\util\TreeUtil::channelList($goodsCategory, $k, "&nbsp;", 'cid', 'pid'));
                    Array_push($cids, $k);
                    /*
                    $_string = '';
                    foreach ($cids as $cid) {
                        $_string .= "find_in_set('" . $cid . "',cids) and ";
                    }
                    $query = substr($_string, 0, strlen($_string) - 4);
                    */
                    $query = "cids regexp '".implode('|',$cids)."'"; //取多对对分类两种套路
                    $brand = $this->where($query)->where('brand_status',1)->order('brand_sort')->column('*','brand_id');
        /*            echo $this->getLastSql();
                    echo ("\r\n");*/

                    $brandList=[];
                    foreach($brand as $key=>$value){
                        $brandList[$key] = format_brand_content($value);
                    }
                    $v['brand'] = $brandList;
                    $cidBrand[$k] = $v;
                }
            }
        }
        cache('brand_cid'.$siteId,$cidBrand);
    }

    /**
     * 获取指定栏目下品牌
     * @param $cid
     */
    public function getBrandByCid($siteId,$cid){
        $brandList = cache('brand_cid'.$siteId);
        return $brandList[$cid];
    }
}
